package com.grape.action.base;


import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.grape.domain.base.Courier;
import com.grape.service.base.CourierService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller
@Namespace("/courier")
@Scope("prototype")
@ParentPackage("json-default")
public class CourierAction extends ActionSupport implements ModelDriven<Courier> {
	private static final long serialVersionUID = 1L;

	// 所有未关联定区的快递员
	@Action(value="findNoAssociation", results=@Result(name="success",type="json"))
	public String findNoAssaciation() {
		List<Courier> lc = cs.findNoAssaciation();
		ServletActionContext.getContext().getValueStack().push(lc);
		return SUCCESS;
	}
	
	
	// 最简单的保存
	@Action(value = "save", results = @Result(name = "success", location = "/pages/base/courier.html", type = "redirect"))
	public String save() throws UnsupportedEncodingException {
		cs.save(courier);

		return SUCCESS;
	}

	// 全部分页
	@Action(value = "findAll", results = @Result(name = "success", type = "json"))
	public String findAll() {
		Pageable pageable = new PageRequest(page - 1, rows);
		
		Specification<Courier> sc = new Specification<Courier>() {
			@Override
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				
				// 条件集合
				List<Predicate> list = new ArrayList<>();
				
				// 条件1：指定员工号
				if (StringUtils.isNotBlank(courier.getCourierNum())) {
					Predicate p1 = cb.equal(root.get("courierNum").as(String.class), courier.getCourierNum());
					list.add(p1);
				}
				
				// 条件二：指定公司，模糊查询
				if (StringUtils.isNotBlank(courier.getCompany())) {
					Predicate p2 = cb.like(root.get("company").as(String.class),"%"+courier.getCompany()+"%");
					list.add(p2);
				}
				
				// 条件三：快递员类型
				if (StringUtils.isNotBlank(courier.getType())) {
					Predicate p3 = cb.equal(root.get("type").as(String.class), courier.getType());
					list.add(p3);
				}
				
				
				// 条件四：收派标准名称，模糊查询
				if (courier.getStandard()!=null && StringUtils.isNotBlank(courier.getStandard().getName())) {
					Join<Object, Object> standardRoot = root.join("standard",JoinType.INNER);
					Predicate p4 = cb.like(standardRoot.get("name").as(String.class), "%"+courier.getStandard().getName()+"%");
					list.add(p4);
				}
				
				// 将所有条件融合,数组里只能写0,别想太多								
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		
		Page<Courier> page = cs.findAll(sc, pageable);
		Map<String, Object> mp = new HashMap<>();
		mp.put("total", page.getTotalElements());
		mp.put("rows", page.getContent());

		ServletActionContext.getContext().getValueStack().push(mp);
		return SUCCESS;
	}
	
	
	@Action(value="delbatch",results=@Result(name="success",location="/pages/base/courier.html",type="redirect"))
	public String delBatch() {
		cs.delBatch(ids);
		
		return SUCCESS;
	}
	
	
	@Action(value="restorebatch",results=@Result(name="success",location="/pages/base/courier.html",type="redirect"))
	public String restoreBatch() {
		cs.restoreBatch(ids);
		
		return SUCCESS;
	}
	
	
	@Action(value="fa",results=@Result(name="success",type="json"))
	public String fa() {
		List<Courier> lc = cs.fa();
		ServletActionContext.getContext().getValueStack().push(lc);
		
		return SUCCESS;
	}
	
/*	@Action(value="fa",results=@Result(name="success",type="json"))
	public String fa() {
		cs.fa();
		
		
		return SUCCESS;
	}*/
	

	@Autowired
	private CourierService cs;
	private Courier courier = new Courier();

	@Override
	public Courier getModel() {
		return courier;
	}

	private Integer page;
	private Integer rows;

	public void setPage(Integer page) {
		this.page = page;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
	}
	
	private String ids;

	public void setIds(String ids) {
		this.ids = ids;
	}
	
}
